using System;
using System.Collections.Generic;
using System.Text;
using Evolution.Engine.GUI.Components;
using Microsoft.Xna.Framework;
using Evolution.Engine.Managers;
using Microsoft.Xna.Framework.Graphics;
using Evolution.Engine.Objects;

namespace Evolution.GUI.Components
{
    public class Caption : Control
    {
        private string _Title;
        private bool _CloseButtonEnabled = true;
        private Pushbutton _CloseButton;
        private Texture2d _LeftImage;
        private Texture2d _BodyImage;
        private Texture2d _RightImage;

        public Texture2d RightImage { get { return _RightImage; } set { _RightImage = value; } }
        public Texture2d BodyImage { get { return _BodyImage; } set { _BodyImage = value; } }
        public Texture2d LeftImage { get { return _LeftImage; } set { _LeftImage = value; } }
        public Pushbutton CloseButton { get { return _CloseButton; } set { _CloseButton = value; } }
        public bool CloseButtonEnabled { get { return _CloseButtonEnabled; } set { _CloseButtonEnabled = value; } }
        public string Title { get { return _Title; } set { _Title = value; } }

        public Caption(Form parent)
            : base(parent)
        {
            LoadImages();
            _CloseButton = Pushbutton.CreateCloseButton("form_CloseButton", "", new Vector2(_Size.Width, 1), new Vector2(15, 15), parent);
            _CloseButtonEnabled = true;

            _CloseButton.OnClick += new ButtonClick(_CloseButton_OnClick);
        }

        public void _CloseButton_OnClick(object sender)
        {
            _Parent.Visible = false;
            _Parent.Disable();
        }
        private void LoadImages()
        {
            _LeftImage = CreateImage("Content/GUI/Caption/LeftImage", new Vector2(0, 0), new Vector2(18, 31));
            _BodyImage = CreateImage("Content/GUI/Caption/BodyImage", new Vector2(0, 0), new Vector2(18, 31));
            _RightImage = CreateImage("Content/GUI/Caption/RightImage", new Vector2(0, 0), new Vector2(45, 31));
        }
        private Texture2d CreateImage(string asset, Vector2 position, Vector2 size)
        {
            Texture2d tmp = new Texture2d(position, size, asset);
            tmp.LoadContent();
            return tmp;
        }
        public override void Update(GameTime gameTime)
        {

        }
        public override void Draw(GameTime gameTime)
        {
            _LeftImage.Draw(gameTime);
            _BodyImage.Draw(gameTime);
            _RightImage.Draw(gameTime);
            if (_CloseButtonEnabled) _CloseButton.Draw(gameTime);
            DrawTitle();
        }
        private void DrawTitle()
        {
            float height = FontManager.Instance["DialogTitle"].MeasureString(_Title).Y;

            TextManager.Instance.Write("DialogTitle", _Title,
                new Vector2(_Location.X + 15, _Location.Y + ((_Size.Height / 2) - (height / 2)) + 1),
                Color.White
            );
        }
        public override void OnMouseDown(MouseEventArgs e)
        {
            if (_CloseButtonEnabled) _CloseButton.OnMouseDown(e);
        }
        public override void OnMouseUp(MouseEventArgs e)
        {
            if (_CloseButtonEnabled) _CloseButton.OnMouseUp(e);
        }
        public void OnDragging(MouseEventArgs e)
        {
            Rectangle rec = new Rectangle((int)_Location.X, (int)_Location.Y, (int)_Size.Width, (int)_Size.Height);
            if (rec.Contains(e.MouseState.X, e.MouseState.Y))
            {
                MouseManager.Instance.SetMousePointer("Content/GUI/Mouse/MovePointer");
                this._Parent.Location += -InputManager.Instance.MouseMoved;

                e.Handled = true;
            }
        }
        public override void Resize()
        {
            _BodyImage.Size = new Vector2(
                _Size.Width - (_LeftImage.Size.X + RightImage.Size.X) + 39,
                31
            );
        }
        public override void SetLocation()
        {
            _LeftImage.Position = new Vector2(_Location.X, _Location.Y);
            _BodyImage.Position = new Vector2(_Location.X + _LeftImage.Size.X, _Location.Y);
            _RightImage.Position = new Vector2(_Location.X + _Size.Width - 6, _Location.Y);
            _CloseButton.Location = new Vector2(_Size.Width + 2, -24);
        }
    }
}
